package org.vehicle.iface;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.TempPersonInfo;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.TempPersonInfoService;
import org.vehicle.service.TripService;
import org.vehicle.service.VehicleInfoService;
import org.vehicle.vo.PersonInfoVO;

@Controller
@RequestMapping("interface/person")
@Transactional
public class PersonController {
	@Autowired
	private TempPersonInfoService tempPersonInfoService;
	@Autowired
	private TripService tripService;
	@Autowired
	private VehicleInfoService vehicleInfoService;
	/**
	 * 添加人员
	 * @param kcTripInfoVO
	 * @return
	 */
    @RequestMapping("addPerson")
    @ResponseBody
    public Map<String, Object> addPersons(@RequestBody PersonInfoVO personInfoVO) {
    	Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap=new HashMap<String, Object>();
			paramMap.put("deviceSN", personInfoVO.getDeviceCode());
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByCode(paramMap);
			
			TripInfo tripInfo=new TripInfo();
			tripInfo.setVehicleLicense(vehicleInfo.getVehicleLicense());
			tripInfo= tripService.findTripInfoByVehicleLicense(tripInfo);
    		if(tripInfo==null) {
    			retMap.put("state", 0);
    			retMap.put("msg", "行程未开始");
    			return retMap;
    		}
    		if("0".equals(personInfoVO.getReportType())){
    			if(personInfoVO.getPersonId()==null&&StringUtils.isEmpty(personInfoVO.getCertificateCode())){
    				retMap.put("state", 1);
        			retMap.put("msg", "操作成功");
        			return retMap;
        		}
    		}
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			TempPersonInfo tempPersonInfo=new TempPersonInfo();
			tempPersonInfo.setDeviceCode(personInfoVO.getDeviceCode());
			tempPersonInfo.setCertificateCode(personInfoVO.getCertificateCode());
			tempPersonInfo.setPersonType(1);
			tempPersonInfo.setPersonName(personInfoVO.getName());
			tempPersonInfo.setGender(personInfoVO.getGender());
			if(!StringUtils.isEmpty(personInfoVO.getBirthday())){
				tempPersonInfo.setBirthday(sdf.parse(personInfoVO.getBirthday()));
			}
			tempPersonInfo.setNationality(personInfoVO.getNationality());
			tempPersonInfo.setAddress(personInfoVO.getAddress());
			tempPersonInfo.setCertificateCodeEffectiveDate(personInfoVO.getCertificateCodeEffectiveDate());
			tempPersonInfo.setSignOrganization(personInfoVO.getSignOrganization());
			tempPersonInfo.setPersonFaceBase64(personInfoVO.getPersonFaceBase64());
			tempPersonInfo.setCertificateFaceBase64(personInfoVO.getCertificateFaceBase64());
			tempPersonInfo.setPersonFaceData(personInfoVO.getPersonFaceData());
			tempPersonInfo.setState(1);
			tempPersonInfo.setCreateDate(new Date());
			
			if(personInfoVO.getPersonId()!=null){
				TempPersonInfo tempPersonInfo2=tempPersonInfoService.selectByPrimaryKey(personInfoVO.getPersonId());
				tempPersonInfo2.setPersonInfoId(null);
				tempPersonInfo2.setPersonType(3);//人脸打卡
				Map<String, Object> paraMap =new HashMap<String, Object>();
				paraMap.put("certificateCode", tempPersonInfo2.getCertificateCode());
				paraMap.put("deviceCode", tempPersonInfo2.getDeviceCode());
				paraMap.put("personType", 3);
				TempPersonInfo temppersoninfo3=tempPersonInfoService.findTempPersonByCertificateCodeAndDeviceCode(paraMap);
				if(temppersoninfo3==null||(new Date().getTime()-temppersoninfo3.getCreateDate().getTime()>60000)){
					tempPersonInfo2.setCreateDate(new Date());
					tempPersonInfo2.setState(1);
					tempPersonInfoService.insertSelective(tempPersonInfo2);
				}
			}else{
				if(StringUtils.isEmpty(personInfoVO.getCertificateCode())){
					tempPersonInfo.setPersonType(4);
					tempPersonInfo.setCertificateCode(UUID.randomUUID().toString().replace("-", ""));
					tempPersonInfoService.insertSelective(tempPersonInfo);
				}else{
					Map<String, Object> paraMap =new HashMap<String, Object>();
					paraMap.put("certificateCode", personInfoVO.getCertificateCode());
					paraMap.put("deviceCode", personInfoVO.getDeviceCode());
					TempPersonInfo temppersoninfo2=tempPersonInfoService.findTempPersonByCertificateCodeAndDeviceCode(paraMap);
					//判断是否存在身份证打卡
					if(temppersoninfo2==null){
						tempPersonInfoService.insertSelective(tempPersonInfo);
					}
				}
			}
			List<Map<String, Object>> personFaceDataList=tempPersonInfoService.findPersonFaceDataListByDeviceCode(personInfoVO.getDeviceCode());
			retMap.put("state", 1);
			retMap.put("msg", "添加成功");
			retMap.put("personFaceDataList", personFaceDataList);
		}catch(Exception e){
			e.printStackTrace();
			retMap.put("state", 0);
			retMap.put("msg", "添加失败");
		}
        return retMap;
    }
}
